<?php
namespace app\api\controller;

use app\api\model\Banner as BannerModel;
use app\api\validate\ProductValidate;
use think\Controller;
use app\api\model\Product as ProductModel;

/**
 * 商品
 * Class Banner
 * @package app\api\controller
 */
class Product extends Common
{
    /**
     * 获取最近新品
     */
    public function getNewList($count = 15)
    {
        (new ProductValidate('getNewList'))->goCheck();
        $products = ProductModel::getProductNewList($count);

        if ($products->isEmpty())
        {
            T(200000);
        }

        $products = $products->hidden(['summary']);
        return $this->response($products);
    }

    /**
     * 根据分类ID获取商品列表
     * @param $category_id
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \app\lib\exception\ThrowError
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getAllByCategory($categoryId)
    {
        (new ProductValidate('getAllByCategory'))->goCheck();
        $products = ProductModel::getByCategory($categoryId);

        if ($products->isEmpty())
        {
            T(200001);
        }

        $products = $products->hidden(['summary']);
        return $this->response($products);
    }

    /**
     * 获取商品详情
     * @param $productId
     * @return \think\response\Json
     * @throws \app\lib\exception\ThrowError
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail($productId)
    {
        (new ProductValidate('detail'))->goCheck();
        $detail = ProductModel::getDetail($productId);
        if (!$detail)
        {
            T(200002);
        }
        return $this->response($detail);
    }
}
